<?php
/**
 * ServiceController
 *
 * PHP Version 5.4
 *
 * @author liuxu <liuxuphp@icesmart.cn>
 */

namespace Api\Controller;

use Model\AdModel;
use Model\LiveSettingModel;
use Model\LiveUrlModel;
use Model\RoomModel;
use Think\Controller;

class ServiceController extends BaseController
{

    /**
     * 获取服务器流通道列表
     * @http-method GET
     */
    public function findStreams()
    {
        $url = C('STREAM_ADDRESS').'/api/v1/streams';
        $content = file_get_contents($url);
        $result = json_decode($content);
        $code = $result->code;

        if($code == 0){
            $data = $this->object_to_array($result->streams);
            foreach($data as $k => $v){
                if($v['publish']['active'] == true){
                    $data[$k]['publish']['active'] = 1;
                }else{
                    $data[$k]['publish']['active'] = 0;
                }
            }
            return $data;
        }else{
            return NULL;
        }
    }

    /**
     * 获取指定服务器流通道
     * @http-method GET
     */
    public function getStream($channel)
    {
        $stream = null;
        $streamList = $this->findStreams();
        if(!empty($streamList)){
            foreach($streamList as $sl){
                if($sl['name'] == $channel){
                    $stream = $sl;
                    break;
                }
            }
            return $stream;
        }else{
            return null;
        }
    }

    /**
     * 关闭流通道
     * @http-method DELETE
     */
    public function offStream($cid)
    {
        $url = C('STREAM_ADDRESS').'/api/v1/clients/'.$cid;
        $result = $this->callInterfaceCommon($url, 'DELETE', null, null);
        $result = json_decode($result);
        return $result->code;
    }

    /**
     * 连接流通道返回信息
     * @http-method GET
     */
    public function connect()
    {
        $data = json_decode(file_get_contents('php://input'));
        $updateTime = date('Y-m-d H:i:s');

        $LiveUrlModel = new LiveUrlModel();
        $LiveUrlInfo = $LiveUrlModel->where('channel = "'.$data->stream.'"')->find();

        $roomModel = new RoomModel();
        $resultData['status'] = 1;
        $resultData['lastIp'] = $data->ip;
        $resultData['lastTime'] = $updateTime;
        $result = $roomModel->where('id = '.$LiveUrlInfo['roomId'])->save($resultData);



        //验证直播总闸是否开启
        if($LiveUrlInfo['status'] == 1){
            echo 0;
        }
    }

    /**
     * 断开流通道返回信息
     * @http-method GET
     */
    public function disConnect()
    {
        $data = json_decode(file_get_contents('php://input'));

        //关闭直播间
        $LiveUrlModel = new LiveUrlModel();
        $LiveUrlInfo = $LiveUrlModel->where('channel = "'.$data->stream.'"')->find();
        $roomModel = new RoomModel();
        $result = $roomModel->where('id = '.$LiveUrlInfo['roomId'])->setField('status', 0);

        //关闭该直播间广告
        $bannerModel = new AdModel();
        $bannerInfo = $bannerModel->where('roomId = '.$LiveUrlInfo['roomId'])->find();
        if($bannerInfo){
            $bannerModel->where('id = '.$bannerInfo['id'])->setField('status', 0);
        }

        if($result){
            echo 0;
        }
    }
} 